package controller;

import dao.BookAdminDao;
import dao.UserAdminDao;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import model.MemberUser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "AdminUserServlet", value = "/admin/AdminUser")
public class AdminUserServlet extends HttpServlet {
    public static final Logger logger = LogManager.getLogger(AdminUserServlet.class);

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method") == null ? "" : request.getParameter("method");
        RequestDispatcher requestDispatcher;
        ServletContext servletContext = this.getServletContext();
        UserAdminDao userAdminDao = new UserAdminDao();

        // 用户列表
        if ("userList".equals(method)) {
            try {
                ArrayList<MemberUser> userList = userAdminDao.getAllUser(10);
                request.setAttribute("userList", userList);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            requestDispatcher = servletContext.getRequestDispatcher("/admin/deal/user_list.jsp");
            requestDispatcher.forward(request, response);
        }

        // 添加用户
        if ("addUser".equals(method)) {
            MemberUser memberUser = (MemberUser) request.getAttribute("addUser");
            if (userAdminDao.addUser(memberUser)) {
                request.getSession().setAttribute("addUser_message", "用户添加成功");
                response.sendRedirect("/admin/user_add.jsp");
            } else {
                request.getSession().setAttribute("addUser_message", "用户添加失败");
                response.sendRedirect("/admin/user_add.jsp");
            }
        }
        // 删除用户
        if ("delUser".equals(method)) {
            int userId = Integer.parseInt(request.getParameter("id"));
            logger.info("delUser id = " + userId);
            if (userId == 0) {
                request.getSession().setAttribute("delUser_message", "用户ID非法");
                logger.info("userId is null");
                response.sendRedirect("/admin/user_list.jsp");
            } else if (userAdminDao.delUser(userId)) {
                request.getSession().setAttribute("delUser_message", "用户删除成功");
                response.sendRedirect("/admin/user_list.jsp");
            } else {
                request.getSession().setAttribute("delUser_message", "用户删除失败");
                response.sendRedirect("/admin/user_list.jsp");
            }
        }

        // 修改用户
        if ("modUser".equals(method)) {
            MemberUser memberUser = (MemberUser) request.getAttribute("modUser");
            if (userAdminDao.modUser(memberUser)) {
                request.getSession().setAttribute("modUser_message", "用户修改成功");
                response.sendRedirect("/admin/user_list.jsp");
            } else {
                request.getSession().setAttribute("modUser_message", "用户修改失败");
                response.sendRedirect("/admin/user_list.jsp");
            }
        }

        // 获取用户信息
        if ("getUser".equals(method)) {
            int userId = Integer.parseInt(request.getParameter("id"));
            logger.info("getUser id = " + userId);
            if (userId == 0) {
                request.getSession().setAttribute("delUser_message", "用户ID非法");
            } else {
                ArrayList<MemberUser> userInfo = userAdminDao.getUserById(userId);
                logger.info(userInfo);
                request.setAttribute("userInfo", userInfo);
            }
            requestDispatcher = servletContext.getRequestDispatcher("/admin/user_mod.jsp");
            requestDispatcher.forward(request, response);
        }

        // 搜索用户
        if ("search".equals(method)) {
            MemberUser memberUser = (MemberUser) request.getAttribute("search");
            String username = memberUser.getUsername();
            logger.info("search username like:" + username);
            try {
                ArrayList<MemberUser> userList = userAdminDao.getUser(username, 10);
                request.setAttribute("userList", userList);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            requestDispatcher = servletContext.getRequestDispatcher("/admin/deal/user_list.jsp");
            requestDispatcher.forward(request, response);
        }
    }
}
